﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using FinApp.DataProviders.Configuration;
using FinApp.ViewModels.Base;
using FinApp.Commands.Base;
using Microsoft.Phone.Controls;
using FinApp.DataProviders;
using FinApp.Model;
using System.Device.Location;
using Microsoft.Phone.Tasks;
using System.Linq;

namespace FinApp.ViewModels {
    public class MainViewModel : ViewModelBase {

        #region Members
        private CommandBase newPaymentCommand;
        private CommandBase routeCommand;
        private GeoCoordinate currentPosition;
        #endregion

        #region Constructors
        public MainViewModel() {
            this.newPaymentCommand = new CommandBase(() => {
                var res = App.Current.RootVisual as PhoneApplicationFrame;
                res.Navigate(new Uri("/Views/QRReaderPage.xaml", UriKind.Relative));
            }, true);
            this.routeCommand = new CommandBase(() => {
                BingMapsDirectionsTask directionTask = new BingMapsDirectionsTask();
                directionTask.End = new LabeledMapLocation("Tu oferta mas cercana", this.CommerceItems.FirstOrDefault().CommerceGeoCoordinate);
                directionTask.Show();
            }, true);
            this.CommerceItems = new ObservableCollection<CommerceItemViewModel>();
        }
        #endregion

        #region Properties
        /// <summary>
        /// Obtiene si hay datos cargados
        /// </summary>
        public bool IsDataLoaded {
            get;
            private set;
        }
        /// <summary>
        /// Obtiene la posición actual
        /// </summary>
        public GeoCoordinate CurrentPosition {
            get { return currentPosition; }
            private set { 
                currentPosition = value;
                this.NotifyPropertyChanged("CurrentPosition");
            }
        }
        /// <summary>
        /// Colección para objetos CommerceItemViewModel.
        /// </summary>
        public ObservableCollection<CommerceItemViewModel> CommerceItems { get; private set; }
        /// <summary>
        /// Obtiene el comando de pago
        /// </summary>
        public ICommand NewPaymentCommand {
            get { return newPaymentCommand; }
        }
        /// <summary>
        /// Obtiene el comando de generación de ruta
        /// </summary>
        public CommandBase RouteCommand {
            get { return routeCommand; }
        }
        #endregion

        #region Overrides
        /// <summary>
        /// Carga de datos de los comercios que ofrecen promociones.
        /// </summary>
        public override void LoadData() {
            CommerceProvider.Instance.PositionChanged += new DataProviders.EventArguments.PositionChangedEventHandler(OnPositionChanged);
            CommerceProvider.Instance.CommerceCompleted += new DataProviders.EventArguments.CommerceCompleteEventHandler(OnCommerceCompleted);
            CommerceProvider.Instance.GetCommerces();
        }
        #endregion

        #region Events
        private void OnCommerceCompleted(object sender, DataProviders.EventArguments.CommerceEventArgs e) {

            //foreach (CommerceDataModel commerceItem in e.CommerceDataModel) {
            if (e.CommerceDataModel.AddresModel != null) {
                string commerceAddres = e.CommerceDataModel.AddresModel.Street + " " + e.CommerceDataModel.AddresModel.Number;
                this.CommerceItems.Add(new CommerceItemViewModel() { CommerceId = e.CommerceDataModel.PublicName+" "+commerceAddres, CommerceName = e.CommerceDataModel.PublicName, CommerceAddres = commerceAddres, CommerceGeoCoordinate = new System.Device.Location.GeoCoordinate(e.CommerceDataModel.Location[0], e.CommerceDataModel.Location[1]) });
                //}
                this.IsDataLoaded = true;
            }
        }

        public void OnPositionChanged(object sender, DataProviders.EventArguments.PositionEventArgs e) {
            this.CurrentPosition = e.GeoCoordinate;
        }
        #endregion

    }
}
